package com.dashan.servlets;

import static com.dashan.Constants.FRIENDNAME;
import static com.dashan.Constants.PERMISSION;
import static com.dashan.Constants.RESET;
import static com.dashan.Constants.SUCCESS;
import static com.dashan.Constants.USERNAME;
import static com.dashan.servlets.Parameter.Option.Required;

import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.jdo.JDOObjectNotFoundException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dashan.backend.AccessControlManager;
import com.dashan.backend.DashanException;
import com.dashan.backend.AccessControlManager.Permission;
import com.dashan.backend.datastore.DsFriend;
import com.dashan.backend.datastore.DsUser;
import com.dashan.backend.datastore.UserDao;
import com.dashan.servlets.Parameter.Option;

@SuppressWarnings("serial")
public class OverrideGroupPermissionServlet extends DashanPrivateServlet {

    @Override
    public ArrayList<Parameter> getParameterList() {
        ArrayList<Parameter> res = super.getParameterList();
        res.add(new Parameter(FRIENDNAME, Required, String.class));
        res.add(new Parameter(PERMISSION, Option.Optional, Permission.class));
        res.add(new Parameter(RESET, Option.Optional, boolean.class, 
                              "TRUE to reset group premission."));
        return res;
    }
    
    @Override
    public String getUrlPattern() {
        return "/overridegrouppermission";
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        TreeMap<String, Parameter> params = loadParamters(req);
        UserDao dao = new UserDao();
        try {
            DsUser user = dao.getUser(params.get(USERNAME).stringValue());
            DsFriend f = user.getFriend(params.get(FRIENDNAME).stringValue());
            if (f == null) {
                throw new DashanException("Friend not found");
            }
            
            if (!params.get(RESET).hasValue() && !params.get(PERMISSION).hasValue()) {
                throw new ServletException("One of 'reset' or 'permission' must have value!");
            }
            if (params.get(RESET).hasValue() && params.get(RESET).booleanValue()) {
                AccessControlManager.useGroupPermission(f);
            } else {
                AccessControlManager.overrideGroupPermission(f,
                        AccessControlManager.
                        PermissionSet2Bits(params.get(PERMISSION).permissionSetValue()));
            }
        } catch (DashanException e) {
            throw new ServletException(e.getMessage());
        } catch (JDOObjectNotFoundException e) {
            throw new ServletException(e.getMessage());
        } finally {
            dao.commit();
        }
        
        resp.getWriter().println(SUCCESS);
    }
}
